package org.wmq.positiontoshop.config;

import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.client.ClientConfiguration;
import org.springframework.data.elasticsearch.client.RestClients;
import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;

/**
 * Elasticsearch配置类
 * 用于配置Elasticsearch客户端和相关组件
 */
@Configuration
@EnableElasticsearchRepositories(basePackages = "org.wmq.positiontoshop.repository")
public class ElasticsearchConfig extends AbstractElasticsearchConfiguration {
    
    /**
     * 配置Elasticsearch客户端
     */
    @Override
    @Bean
    public RestHighLevelClient elasticsearchClient() {
        ClientConfiguration clientConfiguration = ClientConfiguration.builder()
                .connectedTo("localhost:9200")
                .withConnectTimeout(1000)
                .withSocketTimeout(30000)
                .build();
        
        return RestClients.create(clientConfiguration).rest();
    }
    
    /**
     * 提供Elasticsearch操作模板
     */
    @Bean
    public ElasticsearchOperations customElasticsearchTemplate() {
        return new ElasticsearchRestTemplate(elasticsearchClient());
    }
}